package com.yoomiito.app.adapter.home.reommend;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.yoomiito.app.R;
import com.yoomiito.app.model.bean.GoodsInfo;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.widget.CouponView;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class HomeHotAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private List<GoodsInfo> datas;
    private HomeRecommendAdapter.OnItemClickLisenter mOnItemClickLisenter;
    private boolean showHotFlag;

    public HomeHotAdapter(Context context, LayoutHelper layoutHelper, List<GoodsInfo> datas) {
        mContext = context;
        mLayoutHelper = layoutHelper;
        this.datas = datas;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_hot, parent, false);
//        RecyclerView.LayoutParams lp  = (RecyclerView.LayoutParams) view.getLayoutParams();
//        lp.leftMargin = DensityUtil.dp2px(12);
//        lp.rightMargin = DensityUtil.dp2px(12);
//        view.setLayoutParams(lp);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
//        int p_12 = DensityUtil.dp2px(12);
//        int p_6 = DensityUtil.dp2px(6);

//        if (position == 0) {
//            itemViewHolder.mParentView.setPadding(p_12,p_12,p_12,p_6);
//            itemViewHolder.mParentView.setBackgroundResource(R.drawable.bg_white_top_l_r_radius_8);
//        } else if (position == datas.size() - 1) {
//            itemViewHolder.mParentView.setBackgroundResource(R.drawable.bg_white_bottom_l_r_radius_8);
//            itemViewHolder.mParentView.setPadding(p_12,p_6,p_12,p_12);
//
//        } else {
//            itemViewHolder.mParentView.setPadding(p_12,p_6,p_12,p_6);
//            itemViewHolder.mParentView.setBackgroundResource(R.color.color_white);
//        }
        itemViewHolder.mBuyTv.setVisibility(View.VISIBLE);
        GoodsInfo info = datas.get(position);
        //设置title
        setTitle(itemViewHolder.titleTv, info.getProductName());

        //优惠券
//        setGiftAndCoupon(itemViewHolder.mCouponLl, itemViewHolder.voucherTv, info.getCouponmoney());
        //奖--佣金  会员以上显示  分享赚同奖
//        if (UserInfoConfig.getRoleId() > LevelType.VIP_0) {
//            setGiftAndCoupon(itemViewHolder.iv_1, itemViewHolder.ll_1, itemViewHolder.voucherTv, "");
//        } else {
//        itemViewHolder.mAwardLl.setVisibility(View.GONE);
//        }
        String goodsPrice = SpannableUtils.handlePrice(info.getSalePrice());
//        String oldPrice = SpannableUtils.handlePrice(info.getItemprice());
//        String price = "¥" + goodsPrice + " ¥" + oldPrice;
//        SpannableString spannableString = new SpannableString(price);
//        spannableString.setSpan(new RelativeSizeSpan(0.65f),0,1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//        spannableString.setSpan(new RelativeSizeSpan(0.65f),(goodsPrice.contains(".")?goodsPrice.indexOf("."):goodsPrice.length())+1,price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//        spannableString.setSpan(new ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_999999)),goodsPrice.length()+2,price.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//        StrikethroughSpan strikethroughSpan = new StrikethroughSpan();
//        spannableString.setSpan(strikethroughSpan, goodsPrice.length()+2, price.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        itemViewHolder.mGoodsPrice.setText(" ¥" +goodsPrice);
//
//        if (SpannableUtils.isEmptyOr0(info.getCouponmoney())){
//            itemViewHolder.mCouponView.setHeaderTextVisible(false);
//            itemViewHolder.mCouponView.setText("特价精选");
//        }else {
//            itemViewHolder.mCouponView.setHeaderTextVisible(true);
//            itemViewHolder.mCouponView.setText("¥" + info.getCouponmoney());
//        }


        itemViewHolder.tvRecommend.setText(info.productPoint);
        ImageLoader.getInstance().into(mContext, info.getBanner(), 4, itemViewHolder.goodsIv, R.drawable.goods_default);
        String sale = "热销" + info.getVolume() + "件";
        itemViewHolder.mGoodsSaleSizeTv.setText(sale);
        itemViewHolder.mBuyTv.setEnabled(true);
        itemViewHolder.mBuyTv.setText("立即抢购");
        itemViewHolder.mBgLeft.setImageResource( R.drawable.bg_pink_left_1);
        itemViewHolder.mBgRight.setImageResource(R.drawable.bg_pink_right_2);
        itemViewHolder.mBuyTv.setTextColor(ResUtil.getColor(R.color.color_white));
        itemViewHolder.mBuyTv.setOnClickListener(v -> {
            if (mOnItemClickLisenter != null) {
                mOnItemClickLisenter.onClick(position, false);
            }
        });
        itemViewHolder.mView.setOnClickListener(v -> {
//            if (!showHotFlag) {
//                return;
//            }
            if (mOnItemClickLisenter != null) {
                mOnItemClickLisenter.onClick(position, false);
            }
        });
//        itemViewHolder.mShareLl.setOnClickListener(v -> {
//            if (mOnItemClickLisenter != null) {
//                mOnItemClickLisenter.onClick(position, true);
//            }
//        });
    }

    private void setTitle(TextView tv, String str) {
        tv.setText(str);
//        tv.append(SpannableUtils.getIconAndText(str, ResUtil.getDrawable(ivResId)));
    }

    public void setShowHotFlag(boolean showHotFlag) {
        this.showHotFlag = showHotFlag;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 9;
    }

    private void setGiftAndCoupon(View view, TextView tv, String value) {
//        boolean isCoupon = TextUtils.isEmpty(value)||"0".equals(value)||"0.0".equals(value)||"0.00".equals(value);
        boolean isCoupon = SpannableUtils.isEmptyOr0(value);
        view.setVisibility(isCoupon ? View.GONE : View.VISIBLE);
        if (!isCoupon) tv.setText("¥" + value);
    }

    private class ItemViewHolder extends RecyclerView.ViewHolder {

        private TextView mGoodsPrice;
        private TextView titleTv;//商品名称
        private ImageView goodsIv;//商品图
        private View mView;
        private TextView mBuyTv;
        private TextView mGoodsSaleSizeTv;
        private CouponView mCouponView;
        private ImageView mBgLeft;
        private ImageView mBgRight;
        private TextView tvRecommend;

        public ItemViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            goodsIv = itemView.findViewById(R.id.goodsPic);
            titleTv = itemView.findViewById(R.id.goodsTitle);
            itemView.findViewById(R.id.awardView).setVisibility(View.GONE);
            mCouponView = itemView.findViewById(R.id.couponView);
            mGoodsPrice = itemView.findViewById(R.id.goodsPrice);
            mGoodsSaleSizeTv = itemView.findViewById(R.id.saleSizeTv);
            mBuyTv = itemView.findViewById(R.id.statusTv);
            mBgLeft = itemView.findViewById(R.id.bg_iv_1);
            mBgRight = itemView.findViewById(R.id.bg_iv_2);
            tvRecommend = itemView.findViewById(R.id.tv_recommend);
        }
    }

    public void setOnItemClickLisenter(HomeRecommendAdapter.OnItemClickLisenter onItemClickLisenter) {
        mOnItemClickLisenter = onItemClickLisenter;
    }

    public interface OnItemClickLisenter {
        void onClick(int position, boolean isShare);
    }
}
